package com.jlu.iml;

import com.jlu.mcloud.communicate.fileio.FileEntity;
import com.jlu.mcloud.communicate.fileio.FileIoHandler;
import com.jlu.utils.Const;
import com.jlu.utils.DBManager;
import com.jlu.utils.MySqlDB;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.logging.Logger;

/**
 * @Author freedom
 * @DATE 2017/5/16 0016 22:27.
 * see
 */
public class FileIOHandlerImpl implements FileIoHandler {

    private static final Logger log = Logger.getLogger(FileIoHandler.class.getName());

    @Override
    public FileEntity readFile(String s) throws IOException {
        FileEntity fileEntity = null;
        try{
            if(s!=null){
                String filePath = Const.FILEPATH + s;
                File file = new File(filePath);
                fileEntity = new FileEntity();
                byte[] buffer = toArrayByte(file);
                fileEntity.setData(buffer);
                fileEntity.setFileName(s);
                fileEntity.setLength(file.length());
            }
        }catch (Exception e )  {
            e.printStackTrace();
        }
        return fileEntity;
    }

    @Override
    public void writeFile(FileEntity fileEntity) throws IOException {
        String fileName = fileEntity.getFileName();
        String deviceID = fileEntity.getNodeId();
        long fileLenth = fileEntity.getLength();
        String filePath = Const.FILEPATH + fileName;
        if (null != fileName) {
             /*把文件信息存到数据库*/
            boolean flag = DBManager.isExit(fileName, deviceID);
            File file = new File(filePath);
            FileOutputStream fos = new FileOutputStream(file);
            if (!flag) {
                String sql = "insert into file(dev_id,file_name,file_len) values('" + deviceID + "'," +
                        "'" + fileName + "','" + fileLenth + "')";
                int returnCode = MySqlDB.update(sql);
                if (returnCode > 0) {
                /*将文件数据保存到服务器目录*/
                    fos.write(fileEntity.getData());
                    fos.close();
                    log.info(fileName + "接收成功");
                }else {
                    log.info("数据存入数据库失败");
                    fos.close();
                }
            }else {
                log.info(fileName+"已经存在");
                fos.close();
            }
        }
    }

    private byte[] toArrayByte(File file) throws IOException {
        FileChannel fileChannel = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileChannel = fileInputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
            while (fileChannel.read(byteBuffer) > 0) {
            }
            return byteBuffer.array();
        } catch (IOException e) {
            throw e;
        } finally {
            if (fileChannel != null) {
                fileChannel.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        }
    }
}
